package com.overseasShopping.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.overseasShopping.util.MyThreadLocal;



public class SessionFactoryUtil {
	private static SessionFactory factory;
	
	static{
		Configuration cfg=new Configuration().configure();
		factory=cfg.buildSessionFactory();
	}
	
	private static Session opSession(){
		return factory.openSession();
	}
	
	private static MyThreadLocal mt=MyThreadLocal.getInstance();
	public static Session getSession(){
		Session session=(Session) mt.get();
		if(session==null){
			session=opSession();
		}
		return session;
	}
	
	public static void closeSession(Session session){
		if(session!=null&&session.isOpen()){
			session.close();
			session = null ;
			mt.set(null);
		}
	}
}
